<template>
  <draw-component
    :drawer-visible="state.show"
    size="510px"
    title="数据纠偏"
    :show-bottom-btn="false"
    @close-drawer="handleClose"
  >
    <search-top-form
      class="search-form"
      :search-form="searchObj.searchForm"
      :search-item="searchObj.searchDetailItem"
      :show-reset="false"
      @handle-search="handleSearch"
    ></search-top-form>
    <table-page-content
      ref="tableRef"
      :loading="state.loading"
      :table-data="state.tableList"
      :table-headers="chooseHeaders"
      :table-height="'52vh'"
      :row-key="'mechanismId'"
      :is-pager="false"
      :is-selection="true"
      :is-slot-num="false"
    >
    </table-page-content>
    <el-form
      ref="formRef"
      :model="state.formData"
      :rules="state.formRules"
      label-width="auto"
      class="my-form-box2"
    >
      <el-form-item label="纠偏债权人:" prop="mechanismIdTo">
        <el-select v-model="state.formData.mechanismIdTo" placeholder="请选择债权人">
          <el-option
            v-for="item in mechanismList"
            :key="item.code"
            :label="`${item.code} - ${item.name}`"
            :value="item.code"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注:" prop="remark" class="all-width">
        <el-input v-model="state.formData.remark" type="textarea" placeholder="请输入备注"></el-input>
      </el-form-item>
    </el-form>
    <template #footBtn>
      <el-button class="btn cancel" @click="handleClose">取消</el-button>
      <el-button class="btn save" type="primary" @click="handleSubmit">纠偏</el-button>
    </template>
  </draw-component>
</template>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus'
import { chooseHeaders } from '../config'
import { searchDetailItem } from '../config'
import { getDataList, dataRedress } from '../api'
import { useProxy } from '@/utils/proxy'
import { PropType } from 'vue'
const props = defineProps({
  mechanismList: {
    type: Array as PropType<any[]>,
    default: () => []
  }
})
const emit = defineEmits(['handleAdded'])
const tableRef = ref()
const formRef = ref<FormInstance>()
const { proxy } = useProxy()
// 搜索对象
const searchObj = reactive({
  searchForm: { mechanismName: null },
  searchDetailItem
})
const state = reactive({
  loading: false,
  isSubmiting: false,
  show: false,
  formData: {
    mechanismIdTo: '',
    remark: ''
  },
  formRules: {
    mechanismIdTo: [{ required: true, message: '请选择债权人', trigger: 'blur' }]
  } as FormRules,
  tableList: []
})
// 搜索
const handleSearch = () => {
  getList()
}
// 关闭回调
const handleClose = () => {
  state.show = false
  state.formData = {
    mechanismIdTo: '',
    remark: ''
  }
  state.tableList = []
  searchObj.searchForm = { mechanismName: null }
}
// 获取列表数据
const getList = () => {
  state.loading = true
  state.tableList = []
  getDataList<any>({ ...searchObj.searchForm })
    .then((res) => {
      state.tableList = res.data
      state.loading = false
    })
    .catch(() => {
      state.loading = false
    })
}
// 提交
const handleSubmit = async () => {
  let selectItem = tableRef.value.getAllSelectItem()
  if (selectItem.length == 0) {
    return proxy.$message.msgError('请至少选择一条数据')
  }
  // 保存
  await formRef.value?.validate(async (valid) => {
    if (valid) {
      if (state.isSubmiting) return
      state.isSubmiting = true
      const params = { ...state.formData } as any
      let index = props.mechanismList.findIndex((v) => v.code == state.formData.mechanismIdTo)
      params.mechanismIdToName = props.mechanismList[index].name
      params.mechanismIdFrom = selectItem.map((v: any) => v.mechanismId)
      params.mechanismIdFromName = selectItem.map((v: any) => v.mechanismName)
      dataRedress(params)
        .then(() => {
          proxy.$message.msgSuccess('纠偏成功')
          state.isSubmiting = false
          handleClose()
          emit('handleAdded')
        })
        .catch(() => {
          state.isSubmiting = false
        })
    }
  })
}
defineExpose({ state, getList })
</script>

<style lang="scss" scoped>
.my-form-box2 {
  margin-top: 9px;
}
</style>
